
from flask import Flask
from flask import render_template
from flask import request

import model

app = Flask(__name__)

@app.route('/')
def Welcome():
    return "Welcome to QianXiang Test Tools."

@app.route('/starttest')
def StartTest():
    mustData = ['place','roundno']
    inData = getAllData( request, mustData)

    if inData['resultFlag'] :
        model.TestStart(inData)
        return "Test will start." 
    else:
        return "Error: parameter lost."

@app.route('/endtest')
def EndTest():
    mustData = ['place','roundno']
    inData = getAllData( request, mustData)

    if inData['resultFlag'] :
        model.TestEnd(inData)
        return "Test End." 
    else:
        return "Error: parameter lost."

@app.route('/checkin')
def EndTest():
    mustData = ['place','terminal']
    inData = getAllData( request, mustData)

    if inData['resultFlag'] :
        return "Success"
    else:
        return "Error: parameter lost."


@app.route('/savedata')
@app.route('/pagetest')
def PageTest():
    mustData = ['place','roundno','url','testtime','loadtime','sent',\
            'received','rescount','errorcount']
    inData = getAllData( request, mustData)

    if inData['resultFlag'] :
        model.PageTest(inData)
        return "Success" 
    else:
        return "Error: parameter lost."


@app.route('/summary')
def Summary():
    time = '2011-09-09'
    return render_template('summary.html', time=time)

@app.route('/status')
def Status():
    return 'ok'

def getAllData( req, mustdata, optionData=[], optionDefault=[] ):
    data = {}
    for k in mustdata :
        data[k] = req.args.get(k,'')
    print data
    data['resultFlag'] = True
    return data 




if __name__ == '__main__':
    app.debug = True
    app.run( host = '0.0.0.0', port=80)

